import { courseGet } from "@/api/course";
import { Auth } from "@/guard/permission";
import { CourseType } from "@/types/course";
import { EllipsisOutlined, PlusOutlined } from "@ant-design/icons";
import type { ActionType, ProColumns } from "@ant-design/pro-components";
import { ProTable, TableDropdown } from "@ant-design/pro-components";
import { Button, Dropdown, Image, Space, Tag } from "antd";
import { useRef } from "react";
let EditBtn = Auth(<a>编辑</a>, ["超级管理员"]);
const columns: ProColumns<CourseType>[] = [
  {
    dataIndex: "index",
    valueType: "indexBorder",
    width: 48,
  },
  {
    title: "课程名称",
    dataIndex: "name",
    copyable: true,
    ellipsis: true,
    tip: "标题过长会自动收缩",
    formItemProps: {
      rules: [
        {
          required: true,
          message: "此项为必填项",
        },
      ],
    },
  },
  {
    disable: true,
    title: "是否收费",
    dataIndex: "isvip",
    filters: true, //将当前字段设为筛选项目
    onFilter: true,
    ellipsis: true,
    render: (_, record) => {
      let color = record.isvip ? "magenta" : "blue";
      return <Tag color={color}>{record.isvip ? "VIP课程" : "免费课程"}</Tag>;
    },
    valueType: "select",
    valueEnum: {
      1: { text: "所有课程" },
      2: { text: "VIP课程" },
      3: { text: "免费课程" },
    },
  },
  {
    disable: true,
    title: "课程封面",
    dataIndex: "poster",
    search: false, //是否作为查询条件
    renderFormItem: (_, { defaultRender }) => {
      return defaultRender(_);
    },
    render: (url: any) => <Image src={url} />,
  },
  {
    title: "创建时间",
    key: "showTime",
    dataIndex: "createdAt",
    valueType: "date",
    sorter: true,
    hideInSearch: true,
  },
  {
    title: "操作",
    valueType: "option",
    key: "option",
    render: (_) => [
      <EditBtn />,
      <a target="_blank" rel="noopener noreferrer" key="view">
        查看
      </a>,
      <TableDropdown
        key="actionGroup"
        menus={[
          { key: "copy", name: "复制" },
          { key: "delete", name: "删除" },
        ]}
      />,
    ],
  },
];

export default () => {
  const actionRef = useRef<ActionType>();
  return (
    <ProTable<CourseType>
      columns={columns}
      actionRef={actionRef}
      cardBordered
      request={async ({ name, isvip } = {}, sort, filter) => {
        console.log({ name, isvip }, sort, filter);
        let res = await courseGet({ name, isvip });
        console.log(res);

        return {
          data: res.data.results, //此数据会直接交给ProTable进行渲染
          success: true,
        };
      }}
      editable={{
        type: "multiple",
      }}
      columnsState={{
        persistenceKey: "pro-table-singe-demos",
        persistenceType: "localStorage",
        onChange(value) {
          console.log("value: ", value);
        },
      }}
      rowKey="id"
      search={{
        labelWidth: "auto",
      }}
      options={{
        setting: {
          listsHeight: 400,
        },
      }}
      form={{
        // 由于配置了 transform，提交的参与与定义的不同这里需要转化一下
        syncToUrl: (values, type) => {
          if (type === "get") {
            return {
              ...values,
              created_at: [values.startTime, values.endTime],
            };
          }
          return values;
        },
      }}
      pagination={{
        pageSize: 5,
        onChange: (page) => console.log(page),
      }}
      dateFormatter="string"
      headerTitle="高级表格"
      toolBarRender={() => [
        <Button
          key="button"
          icon={<PlusOutlined />}
          onClick={() => {
            actionRef.current?.reload();
          }}
          type="primary"
        >
          新建
        </Button>,
        <Dropdown
          key="menu"
          menu={{
            items: [
              {
                label: "1st item",
                key: "1",
              },
              {
                label: "2nd item",
                key: "1",
              },
              {
                label: "3rd item",
                key: "1",
              },
            ],
          }}
        >
          <Button>
            <EllipsisOutlined />
          </Button>
        </Dropdown>,
      ]}
    />
  );
};
